package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingReturn;
import com.itheima.service.OrdersettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "预约设置相关接口")
public class OrdersettingController {

    @Autowired
    private OrdersettingService ordersettingService;

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @ApiOperation("根据日期修改可预约人数")
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        log.info("根据日期修改可预约人数:{}",orderSetting);
        ordersettingService.editNumberByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS,null);
    }

    /**
     * 根据年月查询对应的预约设置信息
     * @param month
     * @return
     */
    @ApiOperation("根据年月查询对应的预约设置信息")
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        log.info("根据年月查询对应的预约设置信息:{}",month);
        List<OrderSettingReturn> list = ordersettingService.getOrderSettingByMonth(month);
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
    }

    /**
     * 下载文件模板
     * @param filename
     * @param response
     * @return
     */
    @ApiOperation("下载文件模板")
    @GetMapping("/download")
    public void downloadTemplate(String filename,HttpServletResponse response){
        log.info("下载文件模板:{}",filename);
        try {
            ordersettingService.downloadTemplate(filename,response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传文件
     * @param excelFile
     * @return
     */
    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile excelFile){
        log.info("上传文件:{}",excelFile);
        try {
            ordersettingService.upload(excelFile);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS,null);
    }
}
